<?php

class cntrlmanagecommunities implements viewController {

	private $area;
	private $side;
	private $dependencies = array("JavaScript" => array("facebox.js", "manageCommunities.js"), "CSS" => array("facebox.css"));
	private $pageName = "";
	private $userObj;
	private $thisID;
	private $thisInst = array();
	private $groupIDs = array();
	
	public function __construct(user &$userObj){

		if( $userObj->getAccessLevel() == -1 ){
			header("Location: home");
			exit;
		}
		
		$this->side = new template('control.manageCommunities');
		$this->area = new template('area.manageCommunities');

		$this->userObj = $userObj;
		
		parse_str( $_GET['act'] );

		$communities = $userObj->getInstitution();

		
		$myCommunities = array();
		
		foreach($communities as $community){
			if($community['admin'] == 1) $myCommunities[$community['instID']] = $community;
			if($community['instID'] == $this->thisID) $this->thisInst = $community;
		}
				
		// If ID hasn't been set we should select the first ID of communities we manage.
		$firstItem = reset($myCommunities);
		$id = isset($id) ? (int) $id : $firstItem['instID'];
		$this->thisID = $id;
		
		// If there's no ID set, set it to the fist community.
		if(count($this->thisInst) == 0) $this->thisInst = $myCommunities[$id];

		$this->renderPage($id);
		$this->renderGroupList($myCommunities);
	}

	private function renderPage($id){
		$id = (int)$id;
		$groups = institution::getInstitutionGroups($this->userObj, $id);
		
		
		$this->area->replaceAll(array(
			"groupName" => $this->thisInst['name'],
			"groupDescription" => $this->thisInst['description'],
			"instID" => $this->thisInst['instID']
		));
		
		
		// Build the table.
		$table = "";
		$select = "";
		
		if(count($groups) > 0){
			$table = "<h4 class='groupListHeader'>Group</h4>";
		
			foreach($groups as $group){	
				array_push($this->groupIDs, $group['gID']);		
				$table .= "<div class='grouplistItem item" . $group['gID'] . "'>" . $group['name'] . "</div>\n";
				$select .= "<option value='" . $group['gID'] . "'>" . $group['name'] . "</option>\n";
			}
		} else {
			$table = "";
		}
		
		// Get a list of managed groups that are not in the list in this group.
	
		$toAdd = "";
		$managedGroups = $this->userObj->getManagedGroups(true);
		
		foreach($managedGroups as $myGroup){
			if(in_array($myGroup['gID'], $this->groupIDs) == false && $myGroup['instID'] == 1){
				$toAdd .= "<option value='" . $myGroup['gID'] . "'>" . $myGroup['groupName'] . "</option>\n";
			}
		}
		
		$this->area->replace('currentGroupList', $table);
		$this->area->replace('removeSelect', $select);
		$this->area->replace('addSelect', $toAdd);
	}

	protected function renderGroupList($groups){
		$output = "";
		
		foreach($groups as $managedCommunity){
			if($this->thisID == $managedCommunity['instID']){
				$output .= "<option value='" . $managedCommunity['instID'] . "' selected='selected'>" . $managedCommunity['name'] . "</option>\n";
			} else {
				$output .= "<option value='" . $managedCommunity['instID'] . "'>" . $managedCommunity['name'] . "</option>\n";
			}
		}
		
		$this->side->replace('groupList', $output);
	}

	public function getSide(){
		return $this->side;
	}
	
	public function getArea(){
		return $this->area;
	}
	
	public function getDependencies(){
		return $this->dependencies;
	}

	public function getPageName(){
		return $this->pageName;
	}
	
	public function getTools(){
		return "";
	}

}

?>